package hr.koshnica.android.ui;

import java.util.StringTokenizer;

import hr.koshnica.android.R;
import hr.koshnica.android.database.KoshnicaContract;
import hr.koshnica.android.database.KoshnicaContract.Search;
import hr.koshnica.android.ui.search.LocDescActivity;
import hr.koshnica.android.ui.search.SlidingTabsPagerTest;

import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class MainActivityV2 extends Activity {

	private static final int ACTIVITY_CREATE = 0;

	private EditText mSearchView;
	private ListView lv;
	private Activity activity = this;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_v2);

		lv = (ListView) findViewById(R.id.ListView01);
		mSearchView = (EditText) findViewById(R.id.searchboxMainV2);
		
		lv.setOnItemClickListener(new OnItemClickListener()
		{
		    @Override
		    public void onItemClick(AdapterView<?> a, View view, int position, long id)
		    {
		    	TextView tv = (TextView)view.findViewById(R.id.valueRowSearch2);    	
		    	String path = (String)tv.getText();
		    	StringTokenizer tokens = new StringTokenizer(path, "/");
		    	String first = tokens.nextToken();
		    	String second = tokens.nextToken();
		    	System.out.println("first:" + first);
		    	System.out.println("second:" + second);		    	
		    	onShowItemDetails(second, first);
		    }
		});
		
		lv.setEmptyView(findViewById(R.id.linearLayoutMainV2_3));
		mSearchView.addTextChangedListener(new TextWatcher(){
			@Override
			public void afterTextChanged(Editable s){
				// Abstract Method of TextWatcher Interface.		
			}	
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {	
				// Abstract Method of TextWatcher Interface.		
			}		
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count)	{	
				String str = mSearchView.getText().toString();
				System.out.println(str);			
				
				Cursor crs = getContentResolver().query(Search.CONTENT_URI,
		                null, null, new String[] {
	                    str
	                }, null);
				
				 startManagingCursor(crs);
			        String[] from = new String[] {            
			   			 	Search.TITLE,	            
			   				Search.OVERVIEW,
			   				Search.EXTRA_DATA			   				
			        };
			         
			        int[] to = new int[] {
			                R.id.textListRowSearch21, 
			                R.id.textListRowSearch22,
			                R.id.valueRowSearch2			                
			        };
			        SimpleCursorAdapter resultsAdapter = new SimpleCursorAdapter(getApplicationContext(),
			                R.layout.list_row_search_2, crs, from, to);
			        
			        lv.setAdapter(resultsAdapter);

			        stopManagingCursor(crs);
			}
		});
		

		Button btn = (Button) this
				.findViewById(R.id.btnSearchMainV2);
		btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent i = new Intent(activity, SlidingTabsPagerTest.class);
				startActivity(i);
			}
		});

		btn = (Button) this.findViewById(R.id.btnSocialMainV2);
		btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent i = new Intent(activity, SlidingTabsPagerTest.class);
				startActivity(i);
			}
		});
		
		ImageButton imgBtn = (ImageButton) this.findViewById(R.id.searchbuttonMainV2);
		imgBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent i = new Intent(Intent.ACTION_SEARCH);
				String term = mSearchView.getText().toString();
				i.putExtra(SearchManager.QUERY, term);
				i.setClass(activity, SearchActivity.class);
				startActivity(i);
			}
		});
		
	}
		
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.option_menu_main, menu);
		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch (item.getItemId()) {
		case R.id.itemOptionsMenuMain1:
			createLocation();
			return true;
		case R.id.itemOptionsMenuMain2:
			createEvent();
			return true;
		}

		return super.onMenuItemSelected(featureId, item);
	}

	private void createLocation() {
		Intent i = new Intent(this, LocationEdit.class);
		startActivityForResult(i, ACTIVITY_CREATE);
	}

	private void createEvent() {
		Intent i = new Intent(this, EventEdit.class);
		startActivityForResult(i, ACTIVITY_CREATE);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
		super.onActivityResult(requestCode, resultCode, intent);

	}
	
	private void onShowItemDetails(String id, String path) {
	    		    	
		if (path.equals(KoshnicaContract.PATH_LOCATIONS)) {    		 	    		 
			Intent i = new Intent(this, LocDescActivity.class);    	     
			i.putExtra("index", Integer.parseInt(id));	    	     
			startActivity(i); 	         	
	    } else if (path.equals(KoshnicaContract.PATH_EVENTS)) { 
	    	Intent i = new Intent(this, LocDescActivity.class);
	   	    i.putExtra("index", Integer.parseInt(id));
	   	    startActivity(i);
	    	
	    }          

	    
	}
}